//1. set this to the desired path
cd "~/Desktop/LAP_PSRM_Replication/"

//2. Bring in the Garlick Replication data
use "LAP_Replication_GarlickPlus_30states.dta", clear

//3. Limit the sample to the years with maximum coverage
keep if year >= 2011 & year <= 2014
replace sg_vote = 4 if sg_vote == 5

//4. Create indicators for each state and then set their labels to the abbreviation
replace state = upper(state)
tab state, gen(st_)

label var st_1 "AK"
label var st_2 "AL"
label var st_3  "CA"
label var st_4  "CO"
label var st_5  "HI"
label var st_6  "IA"
label var st_7  "ID"
label var st_8  "IN"
label var st_9  "LA"
label var st_10 "MD"
label var st_11 "ME"
label var st_12 "MI"
label var st_13 "MN"
label var st_14 "MO"
label var st_15 "MS"
label var st_16 "MT"
label var st_17 "ND"
label var st_18 "NJ"
label var st_19 "NM"
label var st_20 "NY"
label var st_21 "OH"
label var st_22 "OK"
label var st_23 "PA"
label var st_24 "RI"
label var st_25 "TN"
label var st_26 "TX"
label var st_27 "UT"
label var st_28 "VA"
label var st_29 "WA"
label var st_30 "WI"

//5. Set an indicator for the upper chamber
gen upper = 0 
replace upper = 1 if chamber == "upper"

//6. Run a regression to estimate the party difference of all those states. NOTE: These coefficients should be read in reference to the ommitted category (Alaska)
eststo clear
eststo: reg party_diff st_2-st_30 i.year i.sg_v i.upper

//7. Table 7
esttab , se label b(2) se(2) nostar wide ///
order(st_11 st_30 st_13 st_16 st_3 st_4 st_6 st_12 st_8 st_21 st_22 st_29 st_26 st_17 st_18 st_10 st_19 st_2 st_14 st_23 st_24 st_1 st_25 st_7 st_20 st_15 st_27 st_9 st_5   st_28   )

//8. Limit the sample to roll call votes
gen votes = 1
keep if sg_v == 2

//9. Calculate the average party difference by state-year
collapse (mean) party_diff (sum) votes, by(state year1)

//10. Merge in the Shor McCarty (2015) data
replace state = upper(state)
merge m:1 state year1 using "LAP_Replication_ShorMcCarty_50states.dta"


//11. Calculate the averages of party difference and ideology for each states
collapse (mean) party_diff *diffs, by(state)
egen avgdiff = rowmean(h_diffs s_diffs)

//12. Figure 4
twoway ///
(scatter party_diff avgd, msymbol(none) mlabel(st) mcolor(gs1%60)) ///
(lfit party_diff avgd), ///
ytitle("Estimated Party Difference") xtitle("Distance in Party Medians (Shor McCarty 2015)") ///
legend(order(2 "Fit"))

